﻿
Partial Class adminnew_Finances
    Inherits Page

    Public finances As List(Of transfers)
    Public taxi_phone As String
    Public taxi_amount As Double

    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)

        Try
            taxi_phone = Request.Form("taxi_phone")
            taxi_phone = CType(IIf(String.IsNullOrEmpty(taxi_phone), "", taxi_phone.Trim().Replace("+", "")), String)

            taxi_amount = CType(Request.Form("taxi_amount"), Double)
        Catch ex As Exception

        End Try

        If Request.QueryString.Keys.Count > 0 Then
            Try
                taxi_phone = Request.QueryString("taxi_phone")
                taxi_phone = CType(IIf(String.IsNullOrEmpty(taxi_phone), "", taxi_phone.Trim().Replace("+", "")), String)
            Catch ex As Exception

            End Try
        End If
        

        Using context As New TaxiEntities()
            
            If String.IsNullOrEmpty(taxi_phone) <> True And taxi_amount > 0 Then
                Dim taxist = context.taxist.FirstOrDefault(Function(a) a.taxi_phone = taxi_phone)

                If IsNothing(taxist) <> True Then
                    Dim transfer = New transfers
                    transfer.transfer_code = "Администратор"
                    transfer.transfer_amount = CType(taxi_amount, Decimal?)
                    transfer.client_id = taxist.taxi_id
                    transfer.transfer_id = Guid.NewGuid
                    transfer.transfer_date = Now.AddHours(3)
                    transfer.txt_id = ""

                    context.transfers.Add(transfer)
                    context.SaveChanges()

                End If
            End If

            finances = context.transfers.Include("taxist").Where(Function(a) String.IsNullOrEmpty(taxi_phone) Or a.taxist.taxi_phone = taxi_phone).OrderByDescending(Function(a) a.transfer_date).Skip(0).Take(10000).ToList()

        End Using
    End Sub

End Class
